package com.limbo.showapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.limbo.showapp.MyApplication;
import com.limbo.showapp.R;
import com.limbo.showapp.base.BaseActivity;
import com.limbo.showapp.bean.HotBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.presenter.MainPresenter;
import com.limbo.showapp.util.SPUtils;
import com.limbo.showapp.util.SweetDialogManager;
import com.limbo.showapp.view.IHttpView;
import com.limbo.showapp.widget.OnMultiClickListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

public class MainActivity extends BaseActivity<IHttpView, MainPresenter> implements IHttpView<HotBean> {

    private RecyclerView recycleView;
    private ImageView iv_head;
    private TextView tv_nickname;

    @Override
    protected MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void initView() {
        iv_head = findViewById(R.id.iv_head);
        tv_nickname = findViewById(R.id.tv_nickname);

        recycleView = findViewById(R.id.recycle_view);
        recycleView.setNestedScrollingEnabled(false);
        recycleView.setLayoutManager(new GridLayoutManager(this, 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        findViewById(R.id.fl_clear).setOnClickListener(onMultiClickListener);
        findViewById(R.id.fl_record).setOnClickListener(onMultiClickListener);
    }

    @Override
    protected void initData() {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.icon_default_head)
                .error(R.mipmap.icon_default_head)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(mContext).load(AppConstant.Url.BASE_URL + SPUtils.getString(this, AppConstant.SP.USER_HEAD_PIC))
                .transition(withCrossFade())
                .thumbnail(0.1f)
                .apply(options)
                .into(iv_head);

        tv_nickname.setText(SPUtils.getString(this, AppConstant.SP.USER_NAME));

        //设置缓存数据
        HotBean hotBean = (HotBean) SPUtils.getObject(this, AppConstant.SP.DATA_MAIN_HOT);
        setHotData(hotBean);

        //获取列表数据
        mPresenter.getHotList();
    }

    View.OnClickListener onMultiClickListener = new OnMultiClickListener() {
        @Override
        public void onMultiClick(View v) {
            switch (v.getId()) {
                case R.id.fl_clear:
                    SweetDialogManager.showCacheClearDialog(mContext);
                    break;
                case R.id.fl_record:
                    break;
            }
        }
    };

    @Override
    public void onSuccess(HotBean hotBean) {
        SPUtils.setObject(this, AppConstant.SP.DATA_MAIN_HOT, hotBean);

        setHotData(hotBean);
    }

    private void setHotData(final HotBean hotBean) {
        if (hotBean == null)
            return;

        recycleView.setAdapter(new CommonAdapter<HotBean.Hot>(this, R.layout.item_main_hot, hotBean.getData()) {
            @Override
            protected void convert(ViewHolder holder, final HotBean.Hot bean, int position) {
                ImageView ivImg = holder.getView(R.id.iv_img);
                TextView tvTime = holder.getView(R.id.tv_time);
                TextView tvTitle = holder.getView(R.id.tv_title);
                TextView tvCount = holder.getView(R.id.tv_count);
                String url = AppConstant.Url.BASE_URL + bean.getImgUrl();

                RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.img_loading)
//                .error(R.drawable.img_error)
                        //设置图片加载的优先级
                        .priority(Priority.HIGH)
                        //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                        .skipMemoryCache(false)
                        //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(mContext).load(url)
                        .transition(withCrossFade())
                        .thumbnail(0.1f)
                        .apply(options)
                        .into(ivImg);

                tvTitle.setText(bean.getTitle());
                tvTime.setText(bean.getUpdateTime());
                tvCount.setText(bean.getUpCount());

                holder.itemView.setOnClickListener(new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        if (bean.getType() == AppConstant.TYPE.TYPE_VIDEO) {
                            //视频
                            String url = MyApplication.getProxy(mContext).getProxyUrl(AppConstant.Url.BASE_URL + bean.getSourceUrl());
                            String name = bean.getTitle();
                            //全屏播放视频
                            JZVideoPlayerStandard.startFullscreen(mContext, JZVideoPlayerStandard.class, url, name);
                        } else if (bean.getType() == AppConstant.TYPE.TYPE_PICTURE) {
                            //图片
                            PictureDisplayActivity.OpenActivity(mContext, bean.getTitle(), bean.getSourceUrl(), bean.getPage());
                        } else if (bean.getType() == AppConstant.TYPE.TYPE_CARTOON) {
                            //漫画 pdf
                            CartoonDispalyActivity.OpenActivity(mContext, AppConstant.Url.BASE_URL + bean.getSourceUrl());
                        } else if (bean.getType() == AppConstant.TYPE.TYPE_STORY) {

                        }
                    }
                });
            }
        });
    }

    public void onError(String error) {

    }

    public static void OpenActivity(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }
}
